#! /usr/bin/env python

# NM64 class
#
# Copyright (C) 2011-2012 Christian T. Steigies <steigies@physik.uni-kiel.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

__author__ = "Christian T. Steigies <steigies@physik.uni-kiel.de>"
__license__ = "GPL License"

## --------------------------------------------------------------------------
import os
import sys
import datetime

#http://docs.python.org/library/sqlite3.html
# SpaceWx uses CentOS5 with Python2.4 and pysqlite2
try:
    from pysqlite2 import dbapi2 as sqlite3
except ImportError:
    try:
        from sqlite3 import dbapi2 as sqlite3
    except ImportError:
        print "SQLite module not found"
        sys.exit(1)

#from tools.datetool import str2datetime

## --------------------------------------------------------------------------
def row2insert(row):
    """convert row from MySQL query to INSERT statement (for sqlite)"""
    
    qs = "INSERT INTO data VALUES (\"" + row[0].strftime("%Y-%m-%d %H:%M:%S") +"\""
    for i in range(1, len(row)):
        qs += ", " + str(row[i])
    qs += ")"

    return qs
## --------------------------------------------------------------------------

class nm64():
    def __init__(self, station, year, month, day, hour):

        self._station = station
        self._year    = year
        self._month   = month
        self._day     = day
        self._hour    = hour

        self.__conn = sqlite3.connect(':memory:')
        #self.__conn = sqlite3.connect('test.sqlite')
        self._c = self.__conn.cursor()
        # Create table
        # NM64 BIN and NMDA have different data orders... 
        # NMDA format
        #self._c.execute('''create table data (datetime timestamp, ch01 int, ch02 int, ch03 int, ch04 int, ch05 int, ch06 int, ch07 int, ch08 int, ch09 int, ch10 int, ch11 int, ch12 int, ch13 int, ch14 int, ch15 int, ch16 int, ch17 int, ch18 int, hv1 int, hv2 int, hv3 int, temp1 int, temp2 int, pressure int)''')
        # use NM64 BIN format:
        self._c.execute('''create table data (datetime timestamp, ch01 int, ch02 int, ch03 int, ch04 int, ch05 int, ch06 int, ch07 int, ch08 int, ch09 int, ch10 int, ch11 int, ch12 int, ch13 int, ch14 int, ch15 int, ch16 int, ch17 int, ch18 int, pressure int, temp1 int, temp2 int, hv1 int, hv2 int, hv3 int, dt int, hour int)''')
        self.__conn.commit()


    def station(self):
        return self._station


    def dt(self, min=0):
        return datetime.datetime(self._year, self._month, self._day, self._hour, min)


    def min(self):
        self._c.execute('''SELECT MIN(datetime) FROM data''')
        [dt] = self._c.fetchone() # should return only one line, or None
        #utc = str2datetime(dt)
        utc = str(dt)
        return utc

        
    def max(self):
        self._c.execute('''SELECT MAX(datetime) FROM data''')
        [dt] = self._c.fetchone() # should return only one line, or None
        #utc = str2datetime(dt)
        utc = str(dt)
        return utc

## --------------------------------------------------------------------------


if __name__=="__main__":
    print "This is the NM64_class module."

    #cols = ["ch01", "ch02", "ch03", "ch04", "ch05", "ch06", "ch07", "ch08", "ch09", "ch10", "ch11", "ch12", "ch13", "ch14", "ch15", "ch16", "ch17", "ch18",
    #        "pressure", "temp1", "temp2", "hv1", "hv2", "hv3", "dt", "hour"]
    cols = []
    for i in range(1,18+1):
        cols.append("ch%02i"%(i))
    cols.append("pressure")
    for i in range(1,2+1):
        cols.append("temp%01i"%(i))
    for i in range(1,3+1):
        cols.append("hv%01i"%(i))
    cols.append("dt")
    cols.append("hour")

    print cols

#    for i in cols:
        
